package com.sunnada.edu.system.upload.controller;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.alibaba.fastjson.JSONObject;
import com.sunnada.edu.system.pub.common.BasicController;
import com.sunnada.edu.system.pub.common.Plupload;
import com.sunnada.edu.system.pub.dao.Page;
import com.sunnada.edu.system.pub.service.PubMethods;
import com.sunnada.edu.system.pub.util.Utils;
import com.sunnada.edu.system.sysrole.bean.Role;
import com.sunnada.edu.system.upload.bean.Upload;
import com.sunnada.edu.system.upload.service.UploadService;


@Controller
@RequestMapping("/uploadController")
public class UploadController extends BasicController {
    @Resource
    UploadService uploadService;

	/**附件首页*/
	@RequestMapping("/uploadMain")
	public String uploadMain(HttpServletRequest request) { 
		String moduleId=request.getParameter("moduleId");
		request.setAttribute("pathValue", PubMethods.getPathValue(moduleId));
		return "system/upload/uploadMain";
	}
	/**附件查询*/
	@RequestMapping("/uploadMainSearch")
	public void uploadMainSearch(HttpServletRequest request,HttpServletResponse response) { 
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Upload.class, params);
		Page page = new Page(); 
		page.setRowCount(uploadService.getUploadCots(params));
		page.setPageMap(request, page, params);
		List<?> userList = uploadService.findUpload(params);
		JSONObject json = new JSONObject();		
		json.put("list", userList);
		json.put("params", params); 
		printInfo(response,json);
	}
	
	@RequestMapping("/upload")
	public String upload(HttpServletRequest request) { 
		return "system/upload/upload";
	}
    /**
     * explain 文件上传
     * @param response
     * @param request
     * @return String
     */
    @RequestMapping("/uploadFile")
    public void materialUpload(Plupload plupload, HttpServletResponse response,
            HttpServletRequest request) {
    	 HashMap<String, String> params = new HashMap<String, String>();
         setReqParams(request, null, params);
        plupload.setRequest(request);
        String adType=plupload.getAdType();
        JSONObject json = new JSONObject();
        String recId="";
        try {
            recId=uploadService.materialUpload(plupload,params);
            //素材添加成功，返回素材主键RecId
            json.put("recId", recId);
            json.put("adType", adType);
            json.put("msg", "上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            printInfo(response, (Plupload.setErrorJson(100, e.getMessage())));
        }
        printInfo(response, json);
    }
	/**附件删除*/
	@RequestMapping("/uploadDel")
	public void uploadDel(HttpServletRequest request,HttpServletResponse response) { 
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Upload.class, params);
		params.put("recIds",Utils.getSqlSplitStr(params.get("recIds")));
		String msg = uploadService.uploadDel(params);
		printInfo(response,msg);
	}
    
    

    /**
     * 保存证明文件
     * 
     * @param response
     * @param request
     */
    @RequestMapping("/proveFileSave")
    public void proveFileSave(HttpServletResponse response, HttpServletRequest request) {
        HashMap<String, String> params = new HashMap<String, String>();
        setReqParams(request, null, params);
        String recId = params.get("recId");
        // 创建一个通用的多部分解析器
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        // 判断 request 是否有文件上传,即多部分请求
        if (multipartResolver.isMultipart(request)) {
            // 转换成多部分request
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
            // 取得request中的所有文件名
            Iterator<String> iter = multiRequest.getFileNames();
            while (iter.hasNext()) {
                // 取得上传文件
                MultipartFile file = multiRequest.getFile(iter.next());
                // 存文件
               // String savePath = materialService.FileSave(file);
                String fileName = file.getOriginalFilename();
                // 存路径

            }
        }

        printInfo(response, msg);
    }

    /**附件下载*/
	@RequestMapping("/download")
	public void download(HttpServletRequest request,HttpServletResponse response) { 
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Upload.class, params);
		params.put("recIds",Utils.getSqlSplitStr(params.get("recIds")));
		 List l = uploadService.findUploadById(params);
			String msg = "";
		    for (int i = 0; i < l.size(); i++) {
		    	HashMap<String, String> h = (HashMap<String, String>) l.get(i);
		    	 msg = h.get("FILE_PATH");
			 	
			}
		printInfo(response,msg);
	}
    
}
